*
* Table T10 T21
*
set varabbrev off, permanently
clear all
use epfipc2006-2019plus.dta
*
* Total expenditure
*
global listtotal gasto1-gasto12 year2-year13 ccaa2-ccaa18 income squaredm othehouse ///
       ttenure2-ttenure6 tsize2-tsize5 pdensity2-pdensity3 residshort nmembers nfemales ///
	   nforeign ndworkers n5_15 n16_24 n25_34 n35_64 n65_79 n80omas nstudies2-nstudies4 ///
	   nactive nincome labinc peninc uneinc capinc agemb femalemb foreignmb unemployedmb inactivemb
*
*
foreach var1 in T10 T21 {
	preserve
	dis in blue "`var1'"
    keep if post==0 & `var1'!=.
	* propensity score estimation with survey weights
    quietly logit `var1' $listtotal  [pweight=factor]
	predict double ps if e(sample)==1, pr
	drop if ps==.
    gen w=(`var1'+(((1-`var1')*ps)/(1-ps)))
	* Weights used for ATET estimation accounting for survey weights
	gen estweight=w*factor
	* imposing common support
	quietly sum ps if `var1'==1
    scalar pmintreat=r(min)
	drop if ps<pmintreat & `var1'==0
	quietly sum ps if `var1'==0
    scalar pmaxuntreat=r(max)
	drop if ps>pmaxuntreat & `var1'==1
	* estimating ATETs
	*
	forvalues cat=1/12 {
	    display in red "`: var la gasto`cat''"
		* estimation using survey weights
	    reg dgasto`cat' `var1' [aweight=estweight], r
		if `cat'==1 {
			count if `var1'==1 & e(sample)==1
			local ntreated=r(N) 
			count if `var1'==0 & e(sample)==1
			local nuntreated=r(N)
			*outreg2 using table`var1', excel dec(2)  adds(number_treated, `ntreated', ///
			number_untreated, `nuntreated') replace
		}
		else {
			* count number of treated in ATET estimation
			count if `var1'==1 & e(sample)==1
			local ntreated=r(N) 
			count if `var1'==0 & e(sample)==1
			local nuntreated=r(N)
			*outreg2 using table`var1', excel dec(2)  adds(number_treated, `ntreated', ///
			number_untreated, `nuntreated')
		}
	}
    reg dgastot `var1' [aweight=estweight],r
	* count number of treated in ATET estimation
	count if `var1'==1 & e(sample)==1
	local ntreated=r(N) 
	count if `var1'==0 & e(sample)==1
	local nuntreated=r(N)
	*outreg2 using table`var1', excel dec(2)  adds(number_treated, `ntreated', ///
	number_untreated, `nuntreated')
	reg dincome `var1' [aweight=estweight],r
	* count number of treated in ATET estimation
	count if `var1'==1 & e(sample)==1
	local ntreated=r(N) 
	count if `var1'==0 & e(sample)==1
	local nuntreated=r(N)
	*outreg2 using table`var1', excel dec(2)  adds(number_treated, `ntreated', ///
	number_untreated, `nuntreated') 
	restore
}
*
* Expenditure per equivalent adult
*
global listtotalaeq gastoaeq1-gastoaeq12 year2-year13 ccaa2-ccaa18 income squaredm othehouse ///
       ttenure2-ttenure6 tsize2-tsize5 pdensity2-pdensity3 residshort nmembers nfemales ///
	   nforeign ndworkers n5_15 n16_24 n25_34 n35_64 n65_79 n80omas nstudies2-nstudies4 ///
	   nactive nincome labinc peninc uneinc capinc agemb femalemb foreignmb unemployedmb inactivemb
*
foreach var1 in T10 T21 {
	preserve
	dis in blue "`var1'"
    keep if post==0 & `var1'!=.
	* propensity score estimation with survey weights
    quietly logit `var1' $listtotalaeq  [pweight=factor]
	predict double ps if e(sample)==1, pr
	drop if ps==.
    gen w=(`var1'+(((1-`var1')*ps)/(1-ps)))
	* Weights used for ATET estimation accounting for survey weights
	gen estweight=w*factor
	* imposing common support
	quietly sum ps if `var1'==1
    scalar pmintreat=r(min)
	drop if ps<pmintreat & `var1'==0
	quietly sum ps if `var1'==0
    scalar pmaxuntreat=r(max)
	drop if ps>pmaxuntreat & `var1'==1
	* estimating ATETs
	forvalues cat=1/12 {
	    display in red "`: var la gasto`cat''"
		* estimation using survey weights
	    reg dgastoaeq`cat' `var1' [aweight=estweight], r
		if `cat'==1 {
			count if `var1'==1 & e(sample)==1
			local ntreated=r(N) 
			count if `var1'==0 & e(sample)==1
			local nuntreated=r(N)
			*outreg2 using tableaeq`var1', excel dec(2)  adds(number_treated, `ntreated', ///
			number_untreated, `nuntreated') replace
		}
		else {
			* count number of treated in ATET estimation
			count if `var1'==1 & e(sample)==1
			local ntreated=r(N) 
			count if `var1'==0 & e(sample)==1
			local nuntreated=r(N)
			*outreg2 using tableaeq`var1', excel dec(2)  adds(number_treated, `ntreated', ///
			number_untreated, `nuntreated')
		}
	}	
    reg dgastotaeq `var1' [aweight=estweight],r
	* count number of treated in ATET estimation
	count if `var1'==1 & e(sample)==1
	local ntreated=r(N) 
	count if `var1'==0 & e(sample)==1
	local nuntreated=r(N)
	*outreg2 using tableaeq`var1', excel dec(2)  adds(number_treated, `ntreated', ///
	number_untreated, `nuntreated')
	reg dincomeaeq `var1' [aweight=estweight],r
	* count number of treated in ATET estimation
	count if `var1'==1 & e(sample)==1
	local ntreated=r(N) 
	count if `var1'==0 & e(sample)==1
	local nuntreated=r(N)
	*outreg2 using tableaeq`var1', excel dec(2)  adds(number_treated, `ntreated', ///
	number_untreated, `nuntreated') 
	restore
}
*
* Budget shares
*
global listtotalbs share1-share12 year2-year13 ccaa2-ccaa18 income squaredm othehouse ///
       ttenure2-ttenure6 tsize2-tsize5 pdensity2-pdensity3 residshort nmembers nfemales ///
	   nforeign ndworkers n5_15 n16_24 n25_34 n35_64 n65_79 n80omas nstudies2-nstudies4 ///
	   nactive nincome labinc peninc uneinc capinc agemb femalemb foreignmb unemployedmb inactivemb
*
foreach var1 in T10 T21 {
	preserve
	dis in blue "`var1'"
    keep if post==0 & `var1'!=.
	* propensity score estimation with survey weights
    quietly logit `var1' $listtotalbs  [pweight=factor]
	predict double ps if e(sample)==1, pr
	drop if ps==.
    gen w=(`var1'+(((1-`var1')*ps)/(1-ps)))
	* Weights used for ATET estimation accounting for survey weights
	gen estweight=w*factor
	* imposing common support
	quietly sum ps if `var1'==1
    scalar pmintreat=r(min)
	drop if ps<pmintreat & `var1'==0
	quietly sum ps if `var1'==0
    scalar pmaxuntreat=r(max)
	drop if ps>pmaxuntreat & `var1'==1
	* estimating ATETs
	forvalues cat=1/12 {
	    display in red "`: var la gasto`cat''"
		* estimation using survey weights
	    reg dshare`cat' `var1' [aweight=estweight], r
		if `cat'==1 {
			count if `var1'==1 & e(sample)==1
			local ntreated=r(N) 
			count if `var1'==0 & e(sample)==1
			local nuntreated=r(N)
			*outreg2 using tablebs`var1', excel dec(4)  adds(number_treated, `ntreated', ///
			number_untreated, `nuntreated') replace
		}
		else {
			* count number of treated in ATET estimation
			count if `var1'==1 & e(sample)==1
			local ntreated=r(N) 
			count if `var1'==0 & e(sample)==1
			local nuntreated=r(N)
			*outreg2 using tablebs`var1', excel dec(4)  adds(number_treated, `ntreated', ///
			number_untreated, `nuntreated')
		}
	}	
    restore
}

*
* Invoke OS command to merge table files
/*
! cat tableT10.txt tableT21.txt tableaeqT10.txt tableaeqT21.txt tablebsT10.txt tablebsT21.txt > table_T10T21.csv
* Delete individual table files
! rm table*.txt	
! rm table*.xml
*/
* The End
